import { useAppStore } from '@/stores/modules/app'

interface FormState {
  username: string
  password: string
}

/**
 * 错误信息
 * @param formState
 * @returns
 */
export function useSignInErrorMsg(formState: FormState) {
  const appStore = useAppStore()

  const accountErrorMsg = ref('')
  const passwordErrorMsg = ref('')

  watch(
    () => appStore.loginErrorResult,
    async val => {
      accountErrorMsg.value = ''
      passwordErrorMsg.value = ''
      if (val) {
        await nextTick()
        if (val.type === 'account') {
          accountErrorMsg.value = val.message
        } else {
          passwordErrorMsg.value = val.message
        }
      }
    }
  )

  watch(
    () => formState.username,
    () => {
      accountErrorMsg.value = ''
    }
  )

  watch(
    () => formState.password,
    () => {
      passwordErrorMsg.value = ''
    }
  )

  return { accountErrorMsg, passwordErrorMsg }
}
